package com.it.system.eduorderservice.config;


import com.mysql.cj.Query;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static org.springframework.amqp.core.ExchangeBuilder.topicExchange;

/**
 * 超时订单
 */
@Configuration
@Slf4j
public class TimeoutOrderMQConfig {

    //正常交换机
    public static final String ORDER_EXCHANGE = "order-exchange";
    //正常队列
    public static final String ORDER_QUEUE = "order-queue";
    //死信交换机
    public static final String DEADLETTER_ORDER_EXCHANGE = "deadletter-order-exchange";
    //死信队列
    public static final String DEADLETTER_ORDER_QUEUE = "deadletter-order-queue";
    //正常路由键
    public static final String ORDER_QUEUE_KEY = "order-queue-routekey";
    //死信路由键
    public static final String DEADLETTER_ORDER_QUEUE_KEY = "deadletter-order-queue-routekey";

    /**
     * 定义正常交换机
     * @return
     */
    @Bean
    public TopicExchange orderExchange() {
        return new TopicExchange(ORDER_EXCHANGE);
    }

    /**
     * 定义正常队列
     */
    @Bean
    public Queue orderQueue(){
        Map<String, Object> args=new HashMap<>();
        args.put("x-dead-letter-exchange",DEADLETTER_ORDER_EXCHANGE);
        args.put("x-dead-letter-routing-key",DEADLETTER_ORDER_QUEUE_KEY);
        return  new Queue(ORDER_QUEUE,true,false,false);
    }

    /**
     * 绑定正常交换机和队列
     */
    @Bean
    public Binding bindCourseSave() {
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(ORDER_QUEUE_KEY);
    }

    /**
     * 死信交换机
     */
    @Bean
    public TopicExchange deadletterOrderExchange() {
        return new TopicExchange(DEADLETTER_ORDER_EXCHANGE);
    }

    /**
     * 死信队列
     */
    @Bean
    public Queue deadletterOrderQueue(){
        return  new Queue(DEADLETTER_ORDER_QUEUE,false,false,false);
    }

    /**
     * 绑定死信交换机和队列
     */
    @Bean
    public Binding bindDeadletterOrderQueue() {
        return BindingBuilder.bind(deadletterOrderQueue()).to(deadletterOrderExchange()).with(DEADLETTER_ORDER_QUEUE_KEY);
    }



}
